import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/activity/activity_model.dart';
import 'package:ls_work/net/response/activity/activity_sign_in_list_data.dart';

import '../../net/response/activity/actvity_recommend_list_data.dart';

class ActivityInfoDetailViewModel extends BaseViewModel {
  final ActvityRecommendListData data;

  List<ActivitySignInListData> oldList = [];

  List<ActivitySignInListData> volunteerList = [];

  ActivityInfoDetailViewModel(BuildContext context, this.data) : super(context);

  var contentList = <TitleValue>[];

  var model = ActivityModel();

  var canEnroll = false;


  ActvityRecommendListData? detailInfo;

  @override
  void onCreated() {
    super.onCreated();
    getDetail();
    // notifyListeners();
  }

  Future<void> getDetail() async {
    showLoadingState();
    try {
      var item =
          await model.getActivityDetail(activityNo: data.activityNo??"",activityNameLike: data.activityName??"");
      await getEnrollOldList();
      detailInfo = item;
      contentList = [
        TitleValue(title: "活动名称：", value: "${item.activityName}"),
        TitleValue(title: "活动内容：", value: "${item.activityContent}", type: 1),
        TitleValue(
          title: "活动类型：",
          value: "${item.activityTypeName}",
        ),
        TitleValue(
          title: "活动开始时间：",
          value: "${item.activityStartTime}",
        ),
        TitleValue(
          title: "活动结束时间：",
          value: "${item.activityEndTime}",
        ),
        TitleValue(
          title: "活动负责人：",
          value: "${item.directorName}",
        ),
        TitleValue(
          title: "报名开始时间：",
          value: "${item.enrollStartTime}",
        ),
        TitleValue(
          title: "报名结束时间：",
          value: "${item.enrollEndTime}",
        ),
        TitleValue(
          title: "签到积分：",
          value: "${item.checkPoints}",
        ),
        TitleValue(
          title: "活动人数：",
          value: "${item.isLimitHumans==1?"${item.limitHumans}":"不限制"}",
        ),
        TitleValue(
          title: "志愿者积分：",
          value: "${item.volunteerPoints}",
        ),
        TitleValue(
          title: "志愿者人数：",
          value: "${item.limitVolunteers}",
        ),
        TitleValue(title: "备注：", value: "${item.remark}", type: 1),
      ];
      if(item.enrollEndTime.isNullOrEmpty()){
        canEnroll = item.status==5||item.status==6;
      }else{
        var end = item.enrollEndTime.toDate();
        canEnroll = item.status==4&&DateTime.now().isBefore(end!);
      }

      showContent();
    } catch (e) {
      showErrorState(NetErrorUtils.getNetError(e));
    }
  }

  Future<void> getEnrollOldList() async {
    oldList = await model.oldSignList(activityId: data.id,rows: 999);
    if(data.isVolunteer==1){
      volunteerList = await model.volunteerSignList(activityId: data.id,rows: 999);
    }
  }
}

class TitleValue {
  String title;
  String? value;

  ///0横行 1纵向
  int type;

  TitleValue({required this.title, required this.value, this.type = 0});
}
